import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Font;
import java.io.*;
import java.util.Scanner;

public class DialogueBox {

	// A flag to specify when the dialogue should be rendered.
	private boolean display;
	private boolean disableOnNextAdvance;
	public boolean isEnabled;
	// Objects to read and hold the lines obtained from a text file.
	private Scanner scanner;
	private FileReader fileIn;
	String s1, s2, s3;
	
	// The dialogue box can hold three lines. These are the absolute
	// y-coordinates for each line.
	final int line1 = 500;
	final int line2 = 530;
	final int line3 = 560;

	/**
	 * The constructor initializes a DialogueBox object, but it will not be
	 * displayed until enable() is called.
	 */
	public DialogueBox()
	{
		display = false;
		disableOnNextAdvance = false;
		s1 = s2 = s3 = null;
	}
	
	/**
	 * draw() renders the dialogue box and lines from the text file onto
	 * the screen.
	 * @param g the graphics object with which to draw
	 */
	public void draw(Graphics2D g)
	{
		if (display)
		{
			// Draw text box
			g.setColor(Color.WHITE);
			g.fillRect(0, 472, 794, 100);
			g.setColor(Color.BLACK);
			g.fillRect(2, 474, 790, 96);
			
			// Draw text from input file
			Font f = new Font("SansSerif", Font.PLAIN, 18);
			g.setFont(f);
			g.setColor(Color.WHITE);

			g.drawString(s1, 10, line1);
			g.drawString(s2, 10, line2);
			g.drawString(s3, 10, line3);
		}
	}
	
	/**
	 * advance() is intended to be called on a key press. If the scanner still
	 * has lines to read from the text file, they will be displayed on the
	 * screen. If there are no more lines, the text box will disappear.
	 */
	public void advance()
	{
		if (disableOnNextAdvance)
		{
			disable();
		}
		else // Populate up to three strings with lines from the text file.
		{
			// There must be a line for s1, otherwise the dialogue box
			// would have been disabled.
			s1 = scanner.nextLine();
			
			if (scanner.hasNextLine())
			{
				s2 = scanner.nextLine();
			}
			else
			{
				s2 = "";
			}
			
			if (scanner.hasNextLine())
			{
				s3 = scanner.nextLine();
			}
			else
			{
				s3 = "";
			}
			
			// If there is no longer a next line to read or s3 is the empty
			// string, then we have finished with this text file and should
			// not render text on the next advance.
			if (s3 == "" || !scanner.hasNextLine())
			{
				disableOnNextAdvance = true;
			}
			
		}
	}
	
	/**
	 * enable() plugs a new text file into the DialogueBox object and prepares to
	 * draw it onscreen.
	 * @param textFile the path to the text file
	 * @throws FileNotFoundException if the path given is invalid
	 */
	public void enable(String textFile) throws FileNotFoundException
	{
		display = true;
		fileIn = new FileReader(textFile);
		scanner = new Scanner(fileIn);
		isEnabled = true;
		advance();
	}
	
	/**
	 * disable() closes the scanner and stops the dialogue box from being
	 * drawn. This method is called from advance() when there are no more
	 * lines to be read.
	 */
	public void disable()
	{
		display = false;
		disableOnNextAdvance = false;
		isEnabled = false;
		s1 = s2 = s3 = null;
		scanner.close();
	}
	
	
}
